//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "BaroneAdesiWhaleyApproximationEngine.h"
using namespace Cephei::QL::Pricingengines::Vanilla;
#include <gen/QL/Processes/GeneralizedBlackScholesProcess.h>
#include <gen/QL/Instruments/StrikedTypePayoff.h>
using namespace Cephei::QL::Processes;
using namespace Cephei::QL::Instruments;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Pricingengines::Vanilla::CBaroneAdesiWhaleyApproximationEngine::CBaroneAdesiWhaleyApproximationEngine (Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ process) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CGeneralizedBlackScholesProcess^ _Cprocess;
    try
    {
#ifdef HANDLE
        _phBaroneAdesiWhaleyApproximationEngine = NULL;
#endif
        _Cprocess = safe_cast<CGeneralizedBlackScholesProcess^> (process);
        _Cprocess->Lock();
        boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>& _process = static_cast<boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>&> (_Cprocess->GetShared ()); 
        _ppBaroneAdesiWhaleyApproximationEngine = new boost::shared_ptr<QuantLib::BaroneAdesiWhaleyApproximationEngine> (new QuantLib::BaroneAdesiWhaleyApproximationEngine ( _process ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprocess != nullptr) _Cprocess->Unlock();
    }
}
Cephei::QL::Pricingengines::Vanilla::CBaroneAdesiWhaleyApproximationEngine::CBaroneAdesiWhaleyApproximationEngine (boost::shared_ptr<QuantLib::BaroneAdesiWhaleyApproximationEngine>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBaroneAdesiWhaleyApproximationEngine = NULL;
#endif
	_ppBaroneAdesiWhaleyApproximationEngine = &childNative;
    
}
Cephei::QL::Pricingengines::Vanilla::CBaroneAdesiWhaleyApproximationEngine::CBaroneAdesiWhaleyApproximationEngine (QuantLib::BaroneAdesiWhaleyApproximationEngine& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phBaroneAdesiWhaleyApproximationEngine = NULL;
#endif
	_ppBaroneAdesiWhaleyApproximationEngine = new boost::shared_ptr<QuantLib::BaroneAdesiWhaleyApproximationEngine> (&childNative);
    
    _BaroneAdesiWhaleyApproximationEngineOwner = owner;
    
}

Cephei::QL::Pricingengines::Vanilla::CBaroneAdesiWhaleyApproximationEngine::CBaroneAdesiWhaleyApproximationEngine (CBaroneAdesiWhaleyApproximationEngine^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBaroneAdesiWhaleyApproximationEngine = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBaroneAdesiWhaleyApproximationEngine = new boost::shared_ptr<QuantLib::BaroneAdesiWhaleyApproximationEngine> (copy->GetShared());
        
    }
}
Cephei::QL::Pricingengines::Vanilla::CBaroneAdesiWhaleyApproximationEngine::CBaroneAdesiWhaleyApproximationEngine (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBaroneAdesiWhaleyApproximationEngine = NULL;
#endif
	if (!t->IsSubclassOf(CBaroneAdesiWhaleyApproximationEngine::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Pricingengines::Vanilla::CBaroneAdesiWhaleyApproximationEngine::CBaroneAdesiWhaleyApproximationEngine (QuantLib::Handle<QuantLib::BaroneAdesiWhaleyApproximationEngine>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phBaroneAdesiWhaleyApproximationEngine = &childNative;
	_ppBaroneAdesiWhaleyApproximationEngine = &static_cast<boost::shared_ptr<QuantLib::BaroneAdesiWhaleyApproximationEngine>>(childNative.currentLink());
    
    _BaroneAdesiWhaleyApproximationEngineOwner = owner;
}
Cephei::QL::Pricingengines::Vanilla::CBaroneAdesiWhaleyApproximationEngine::CBaroneAdesiWhaleyApproximationEngine (QuantLib::Handle<QuantLib::BaroneAdesiWhaleyApproximationEngine> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phBaroneAdesiWhaleyApproximationEngine = &childNative;
	_ppBaroneAdesiWhaleyApproximationEngine = &static_cast<boost::shared_ptr<QuantLib::BaroneAdesiWhaleyApproximationEngine>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Pricingengines::Vanilla::CBaroneAdesiWhaleyApproximationEngine::CBaroneAdesiWhaleyApproximationEngine (QuantLib::BaroneAdesiWhaleyApproximationEngine childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBaroneAdesiWhaleyApproximationEngine = NULL;
#endif
	_ppBaroneAdesiWhaleyApproximationEngine = new boost::shared_ptr<QuantLib::BaroneAdesiWhaleyApproximationEngine> (new QuantLib::BaroneAdesiWhaleyApproximationEngine (childNative));
    
}
#endif

Cephei::QL::Pricingengines::Vanilla::CBaroneAdesiWhaleyApproximationEngine::~CBaroneAdesiWhaleyApproximationEngine ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppBaroneAdesiWhaleyApproximationEngine != NULL)
    {
	    delete _ppBaroneAdesiWhaleyApproximationEngine;
        _ppBaroneAdesiWhaleyApproximationEngine = NULL;
    }
}
Cephei::QL::Pricingengines::Vanilla::CBaroneAdesiWhaleyApproximationEngine::!CBaroneAdesiWhaleyApproximationEngine ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppBaroneAdesiWhaleyApproximationEngine != NULL)
    {
	    delete _ppBaroneAdesiWhaleyApproximationEngine;
    }
}
QuantLib::BaroneAdesiWhaleyApproximationEngine& Cephei::QL::Pricingengines::Vanilla::CBaroneAdesiWhaleyApproximationEngine::GetReference ()
{
    if (_ppBaroneAdesiWhaleyApproximationEngine == NULL) throw gcnew NativeNullException ();
	return **_ppBaroneAdesiWhaleyApproximationEngine;
}
boost::shared_ptr<QuantLib::BaroneAdesiWhaleyApproximationEngine>& Cephei::QL::Pricingengines::Vanilla::CBaroneAdesiWhaleyApproximationEngine::GetShared ()
{
    if (_ppBaroneAdesiWhaleyApproximationEngine == NULL) throw gcnew NativeNullException ();
	return *_ppBaroneAdesiWhaleyApproximationEngine;
}
QuantLib::BaroneAdesiWhaleyApproximationEngine* Cephei::QL::Pricingengines::Vanilla::CBaroneAdesiWhaleyApproximationEngine::GetPointer ()
{
    if (_ppBaroneAdesiWhaleyApproximationEngine == NULL) throw gcnew NativeNullException ();
	return &**_ppBaroneAdesiWhaleyApproximationEngine;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BaroneAdesiWhaleyApproximationEngine>& Cephei::QL::Pricingengines::Vanilla::CBaroneAdesiWhaleyApproximationEngine::GetHandle ()
{
	if (_phBaroneAdesiWhaleyApproximationEngine == NULL)
	{
		_phBaroneAdesiWhaleyApproximationEngine = new Handle<QuantLib::BaroneAdesiWhaleyApproximationEngine> (*_ppBaroneAdesiWhaleyApproximationEngine);
	}
	return *_phBaroneAdesiWhaleyApproximationEngine;
}
#endif
bool Cephei::QL::Pricingengines::Vanilla::CBaroneAdesiWhaleyApproximationEngine::HasNative () 
{
	return (_ppBaroneAdesiWhaleyApproximationEngine != NULL);
}

Cephei::QL::Pricingengines::Vanilla::IBaroneAdesiWhaleyApproximationEngine^ Cephei::QL::Pricingengines::Vanilla::CBaroneAdesiWhaleyApproximationEngine::Calculate::get ()
{
    try
    {
    	(*_ppBaroneAdesiWhaleyApproximationEngine)->calculate ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Pricingengines::Vanilla::CBaroneAdesiWhaleyApproximationEngine::CriticalPrice (Cephei::QL::Instruments::IStrikedTypePayoff^ payoff, Double riskFreeDiscount, Double dividendDiscount, Double variance, Microsoft::FSharp::Core::FSharpOption<Double>^ tolerance)
{
    CStrikedTypePayoff^ _Cpayoff;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cpayoff = safe_cast<CStrikedTypePayoff^> (payoff);
        _Cpayoff->Lock();
        boost::shared_ptr<QuantLib::StrikedTypePayoff>& _payoff = static_cast<boost::shared_ptr<QuantLib::StrikedTypePayoff>&> (_Cpayoff->GetShared ()); 
        QuantLib::DiscountFactor _riskFreeDiscount = (QuantLib::DiscountFactor)ValueHelper::Convert (riskFreeDiscount);
        QuantLib::DiscountFactor _dividendDiscount = (QuantLib::DiscountFactor)ValueHelper::Convert (dividendDiscount);
        QuantLib::Real _variance = (QuantLib::Real)ValueHelper::Convert (variance);
        QuantLib::Real _tolerance = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (tolerance) ? (QuantLib::Real)ValueHelper::Convert (tolerance->Value) : 1e-6); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBaroneAdesiWhaleyApproximationEngine)->criticalPrice ( _payoff,  _riskFreeDiscount,  _dividendDiscount,  _variance,  _tolerance );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cpayoff != nullptr) _Cpayoff->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Pricingengines::Vanilla::IBaroneAdesiWhaleyApproximationEngine^ Cephei::QL::Pricingengines::Vanilla::CBaroneAdesiWhaleyApproximationEngine_Factory::Create (Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ process)
{
    return gcnew CBaroneAdesiWhaleyApproximationEngine ( process);
}
